﻿using System;
using CrmClient;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using MsCrmClientTest.MSCRM;

namespace MsCrmClientTest
{
    public class OrgCrmClient : CrmClientBase
    {
        private class WcfCrmClient : IWcfCrmClient
        {
            private OrganizationServiceClient _client;

            public Guid Create(Entity entity)
            {
                return _client.Create(entity);
            }

            public void Update(Entity entity)
            {
                _client.Update(entity);
            }

            public void Delete(string entityName, Guid id)
            {
                _client.Delete(entityName, id);
            }

            public EntityCollection RetrieveMultiple(QueryBase query)
            {
                return _client.RetrieveMultiple(query);

            }

            public OrganizationResponse Execute(OrganizationRequest request)
            {
                return _client.Execute(request);
            }

            public void Close()
            {
                _client.Close();
            }

            public WcfCrmClient()
            {
                _client = new OrganizationServiceClient();
            }
        }

        private IWcfCrmClient _wcfClient;
        protected override IWcfCrmClient WcfClient
        {
            get
            {
                if (_wcfClient == null)
                    _wcfClient = new WcfCrmClient();
                return _wcfClient;
            }
        }
    }
}
